/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package br.unifor.redes1.odirabel.gui.client;

import br.unifor.redes1.odirabel.ConfigProperties;
import br.unifor.redes1.odirabel.gui.server.Cliente;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author pedro
 */
public class ServidorNoCliente extends Thread{
    JPanelPrincipal panel;
    private ServerSocket serverSocket;
    private Socket socketAux;
    private Cliente clienteAux;
    ServidorNoCliente(JPanelPrincipal aThis) throws IOException {
        this.panel = aThis;
         
            try {
                serverSocket = new ServerSocket(Integer.parseInt(ConfigProperties.getInstance().getProperty("portacliente")));
            } catch (IOException ex) {
              throw ex;
            }
        
    }

    public void run(){
        while(Boolean.TRUE){
            try {
                socketAux = serverSocket.accept();
                clienteAux = panel.getListaUsersPersist().get(panel.getListaUsersPersist().indexOf(new Cliente("", socketAux.getInetAddress().getHostAddress())));
                UsuarioChat userChat = new UsuarioChat(panel.getLogin(),clienteAux,socketAux );
                panel.getChat().getJTabbedPane1().add(userChat,clienteAux.getLogin());
                new Thread(userChat).start();
                panel.getChat().setVisible(Boolean.TRUE);
            } catch (IOException ex) {
               ex.printStackTrace();
            }
        }
        
    }

}
